﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Policy;
using System.Collections;

namespace NMoq.Core
{
    public class DefaultDataProvider:IDataProvider 
    {
        private Dictionary<int, object> MethodReturnCache
        {
            get;
            set;
        }

        public DefaultDataProvider()
        {
            MethodReturnCache = new Dictionary<int , object>();
        }


        #region IDataProvider Members

        public void Write(MethodMessageInfo methodMessageInfo, object retValue)
        {
            int methodHashcode = methodMessageInfo.GetIdentifier<IDataProvider>();
            bool exists = MethodReturnCache.Keys.Contains(methodHashcode);
            if (exists)
                MethodReturnCache.Remove(methodHashcode);

            MethodReturnCache.Add(methodHashcode, retValue); 
        }

        public DataProviderResult Read(MethodMessageInfo methodMessageInfo)
        {
            DataProviderResult result = new DataProviderResult ();
            int methodHashcode = methodMessageInfo.GetIdentifier<IDataProvider>();
            bool exists = MethodReturnCache.Keys.Contains(methodHashcode); 
            result.HasValue = exists ;
            result.ReturnValue =exists ? MethodReturnCache[methodHashcode]:null ; 
            return result;
        }

        #endregion
    }
}
